package com.rjwh.dingdong.client.activity.loginregist;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.mvp.presenter.impl.ForgetPwdPresenter;
import com.rjwh.dingdong.client.mvp.view.ForgetPwdView;
import com.rjwh.dingdong.client.util.ContactPhoneUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 找回密码
 *
 * @author 王博扬
 * @ClassName ForgetPwdActivity
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2015-8-24 下午1:52:24
 * @history 1.YYYY-MM-DD author: description:
 */
public class ForgetPwdActivity extends BaseActivity implements ForgetPwdView, TextWatcher {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.step1_flag_view)
    TextView step1Flag;
    @BindView(R.id.step1_tv_view)
    TextView step1Tv;
    @BindView(R.id.step2_flag_view)
    TextView step2Flag;
    @BindView(R.id.step2_tv_view)
    TextView step2Tv;
    @BindView(R.id.input_reget_pwd_phone_no)
    EditText phoneNoEt;
    @BindView(R.id.input_reget_pwd_code)
    EditText codeInputEt;
    @BindView(R.id.input_reget_pwd_get_code_btn)
    Button getCodeBtn;
    @BindView(R.id.input_reget_pwd_next_btn)
    Button nextBtn;
    @BindView(R.id.reg_pwd_step1_root_view)
    LinearLayout step1RootView;
    @BindView(R.id.input_reget_pwd_new_pwd)
    EditText newPwdEt;
    @BindView(R.id.input_reget_pwd_confrim_new_pwd)
    EditText confirmPwdEt;
    @BindView(R.id.input_reget_pwd_confrim_save_btn)
    Button saveBtn;
    @BindView(R.id.reg_pwd_step2_root_view)
    LinearLayout step2RootView;

    private TimeCount time;
    private String currentPhone = null;
    private String codeInput;
    private ForgetPwdPresenter presenter;
    private int currentStep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forget_pwd_layout);
        ButterKnife.bind(this);
        time = new TimeCount(10 * 6000, 1000);// 构造CountDownTimer对象
        initView();
        presenter = new ForgetPwdPresenter(this, this);
    }

    private void initView() {
        initToolbarHelper();
        setViewWithStep(1);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("忘记密码");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setViewWithStep(int step) {
        if (step == 1) {
            currentStep = 1;
            step1RootView.setVisibility(View.VISIBLE);
            step2RootView.setVisibility(View.GONE);
            step1Flag.setBackgroundResource(R.drawable.orange_circle_pre);
            step1Tv.setTextColor(getResources().getColor(R.color.orange_nor));
            step2Flag.setBackgroundResource(R.drawable.orange_circle_nor);
            step2Tv.setTextColor(getResources().getColor(R.color.common_font_color_3));
            nextBtn.setClickable(false);
            phoneNoEt.addTextChangedListener(this);
            codeInputEt.addTextChangedListener(this);
        } else if (step == 2) {
            currentStep = 2;
            step1RootView.setVisibility(View.GONE);
            step2RootView.setVisibility(View.VISIBLE);
            step1Flag.setBackgroundResource(R.drawable.orange_circle_nor);
            step1Tv.setTextColor(getResources().getColor(R.color.orange_pre));
            step2Flag.setBackgroundResource(R.drawable.orange_circle_pre);
            step2Tv.setTextColor(getResources().getColor(R.color.orange_nor));
            saveBtn.setClickable(false);
            newPwdEt.addTextChangedListener(this);
            confirmPwdEt.addTextChangedListener(this);
        }
    }

    private void doModifyPwd(String passWord) {
        presenter.doModifyPwd(currentPhone, passWord, codeInput);
    }

    private void goToStep2(String inputPhone, String codeInput) {
        presenter.doCheckCode(inputPhone, codeInput);
    }

    private void doGetCode(String inputPhone) {
        presenter.doGetCode(inputPhone);
    }

    @OnClick({R.id.input_reget_pwd_get_code_btn, R.id.input_reget_pwd_next_btn, R.id.input_reget_pwd_confrim_save_btn})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.input_reget_pwd_get_code_btn:
                String inputPhone1 = phoneNoEt.getText().toString();
                if (!ContactPhoneUtils.isMobileNO(inputPhone1)) {
                    showToast("请输入正确的手机号！");
                    return;
                }
                currentPhone = inputPhone1;
                doGetCode(inputPhone1);
                break;
            case R.id.input_reget_pwd_next_btn:
                String inputPhone2 = phoneNoEt.getText().toString();
                codeInput = codeInputEt.getText().toString();
                if (!ContactPhoneUtils.isMobileNO(inputPhone2)) {
                    showToast("请输入正确的手机号！");
                    return;
                }
                goToStep2(inputPhone2, codeInput);
                break;
            case R.id.input_reget_pwd_confrim_save_btn:
                String newPwd = newPwdEt.getText().toString();
                String confirmPwd = confirmPwdEt.getText().toString();
                if (!confirmPwd.equals(newPwd)) {
                    showToast("两次密码不一致！");
                    return;
                }
                doModifyPwd(newPwd);
                break;

            default:
                break;
        }
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }


    @Override
    public void setTimerData() {
        time.start();
    }

    @Override
    public void updataViewWithStep(int currentStep) {
        setViewWithStep(2);
    }

    @Override
    public void finishActivity() {
        this.finish();
    }

    @Override
    public void setGetCodeBtnClick(boolean clickable) {
        getCodeBtn.setClickable(clickable);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkEditText();
    }

    private void checkEditText() {
        if (currentStep == 1) {
            if (!AsStrUtils.isEmpty(phoneNoEt.getText().toString()) && !AsStrUtils.isEmpty(codeInputEt.getText().toString())) {
                nextBtn.setBackgroundResource(R.drawable.long_btn);
                nextBtn.setClickable(true);
            } else {
                nextBtn.setBackgroundResource(R.drawable.long_btn_unable);
                nextBtn.setClickable(false);
            }
        } else if (currentStep == 2) {
            if (!AsStrUtils.isEmpty(newPwdEt.getText().toString()) && !AsStrUtils.isEmpty(confirmPwdEt.getText().toString())) {
                saveBtn.setBackgroundResource(R.drawable.long_btn);
                saveBtn.setClickable(true);
            } else {
                saveBtn.setBackgroundResource(R.drawable.long_btn_unable);
                saveBtn.setClickable(false);
            }
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            getCodeBtn.setText("获取验证码");
            getCodeBtn.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            getCodeBtn.setClickable(false);
            getCodeBtn.setText(millisUntilFinished / 1000 + "秒");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
        time.cancel();
    }
}
